﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using SmallJob.Data;
using SmallJob.Model;
using SmallJob.Services.Interfaces;
using SmallShop.Services.Code;

namespace SmallShop.Services.Implements
{
    public static class UtilityService
    {
        public static List<City> GetCities(){
            using (var db = new SmallJobDB(ConfigurationManager.ConnectionStrings["SmallJobDB"].ConnectionString))
            {
                return (from c in db.Cities
                        select c).ToList();
            }
        }
        public static User GetProperName(String email) {
            using (var db = new SmallJobDB(ConfigurationManager.ConnectionStrings["SmallJobDB"].ConnectionString))
            {
                return (from u in db.Users
                        where u.Email == email || u.FbID==email
                        select u).FirstOrDefault();
            }
        }
        public static void UpdatePremiumUser()
        {
            using (var db = new SmallJobDB(ConfigurationManager.ConnectionStrings["SmallJobDB"].ConnectionString))
            {
                var now = DateTime.Now;
                var users = from u in db.Users
                            where u.Premium && u.PremiumTo <= now
                            select u;
                foreach (var item in users)
                {
                    item.Premium = false;
                }
                db.SaveChanges();
            }
        }
        public static List<Provide> ProvideLoadUser(List<Provide> lstProvide, IUserService userservice)
        {
            foreach (var item in lstProvide)
            {
                item.User = userservice.GetById(item.UserID);
            }
            return lstProvide;
        }
        public static List<Provide> ProvideLoadCategory(List<Provide> lstProvide, ICategoryService cateservice)
        {
            foreach (var item in lstProvide)
            {
                item.Category = cateservice.GetById(item.CategoryID);
            }
            return lstProvide;
        }
        public static User GetLoginUser(IUserService _UserService,System.Security.Principal.IPrincipal User)
        {
            return _UserService.GetByFBID(User.Identity.Name) ?? _UserService.GetByEmail(User.Identity.Name);
        }
        public static List<Request> RequestLoadUser(List<Request> lstRequest, IUserService userservice)
        {
            foreach (var item in lstRequest)
            {
                item.AssignedUser = userservice.GetById(item.AssignedUserID);
            }
            return lstRequest;
        }
        public static List<Request> RequestLoadCategory(List<Request> lstRequest, ICategoryService cateservice)
        {
            foreach (var item in lstRequest)
            {
                item.Category = cateservice.GetById(item.CategoryID);
            }
            return lstRequest;
        }
    }
}
